﻿package com.thorqq.magictimer.timeLoop;

import java.util.Calendar;

import com.thorqq.magictimer.core.TLoopPolicy;
import com.thorqq.magictimer.util.DateUtil;
import com.thorqq.magictimer.util.Util;


public class TLoopPolicyDay extends TLoopPolicy
{
    private static final long serialVersionUID = -6319428876086320225L;
    protected static final int INFINITY_COUNT = -1;
    protected final int PARAM_NUM = 5;

    protected int     mLoopDays = 1;  //循环天数
    protected int     mMaxCount = INFINITY_COUNT; //循环次数
    protected char[]  mDayMask;       //日掩码
    protected Calendar mStartDate;     //开始年月日
    protected int     mLunarFlag = 0;  //mStartDate的阴历标志

    public TLoopPolicyDay(int loopPolicyType, String loopParam)
    {
        super(loopPolicyType, loopParam);
    }

    public TLoopPolicyDay(int id, int displayOrder, int loopPolicyType, String loopParam, int excludeFlag)
    {
        super(id, displayOrder, loopPolicyType, loopParam, excludeFlag);
    }

    @Override
    public String toString()
    {
        return super.toString();    
    }

    @Override
    public String getInfo()
    {
        StringBuffer sff = new StringBuffer("[按天]");
        Calendar c = Calendar.getInstance();
        sff.append(" 从 ");
        if(DateUtil.intervalDays(mStartDate, c) == 0)
        {
            sff.append("今天");
        }
        else if(DateUtil.intervalDays(mStartDate, c) == 1)
        {
            sff.append("明天");
        }
        else if(DateUtil.intervalDays(mStartDate, c) == 2)
        {
            sff.append("后天");
        }
        else if(DateUtil.intervalDays(mStartDate, c) == -1)
        {
            sff.append("昨天");
        }
        else
        {
            sff.append(DateUtil.MillisToYYYYMMDD(mStartDate));
        }
        sff.append(" 开始");
        return sff.toString();
    }

    @Override
    public String getInfoMore()
    {
        StringBuffer sff = new StringBuffer();
        if(mPolicyType == TLoopPolicy.LOOP_POLICY_DAY)
        {
            if(mLoopDays == 1 && mDayMask[0] == '1' && mMaxCount == INFINITY_COUNT)
            {
                sff.append("每天");
            }
            else if(mLoopDays == 1 && mDayMask[0] == '1' && mMaxCount == 1)
            {
                sff.append("仅此一天");
            }
            else if(mLoopDays == 1 && mDayMask[0] == '1' && mMaxCount > 1)
            {
                sff.append("连续").append(mMaxCount).append("天");
            }
            else if(mLoopDays > 1)
            {
                if(mLoopDays > mDayMask.length)
                {
                    return "循环周期无效，请设置启动日" ;
                }
                
                int flag = 0;
                sff.append("周期:").append(mLoopDays).append("天,");
                sff.append("第");
                for(int i = 0; i < mLoopDays; i++)
                {
                    if(mDayMask[i] == '1')
                    {
                        if(flag == 0)
                        {
                            sff.append(i+1);
                            flag = 1;
                        }
                        else
                        {
                            sff.append(",").append(i+1);
                        }
                    }
                }
                sff.append("天启动");
            }            
            
        }
        else if(mPolicyType == TLoopPolicy.LOOP_POLICY_DAY_NDAYS)
        {
            sff.append("持续").append(mLoopDays).append("天");
        }
        
        return sff.toString();
    }

    public void setParam(
            int     loopDays,
            int     maxCount,
            char[]  dayMask,
            Calendar startDate,
            int     lunarFlag)
    {
        mLoopDays  = loopDays;
        mMaxCount  = maxCount;
        mDayMask   = dayMask.clone();
        mStartDate = (Calendar) startDate.clone();
        mLunarFlag = lunarFlag;
        
        this.paramToString();
    }

    @Override
    public boolean parseStringParam()
    {
        String[] array = Util.splitParames(mLoopParam);
        if(array.length != PARAM_NUM)
        {
            Util.log("TimeParam setParam error. param = " + mLoopParam);
            return false;
        }
                    
        mLoopDays  = Util.strToInt(array[0]);
        mMaxCount  = Util.strToInt(array[1]);
        mDayMask   = array[2].toCharArray();
        mStartDate = (Calendar)DateUtil.getCalendar(array[3]).clone();
        mLunarFlag = Util.strToInt(array[4]);

        return true;
    }

    @Override
    public String paramToString()
    {
        StringBuffer sff = new StringBuffer();
        sff.append(Util.intToStr(mLoopDays))
           .append(Util.PARAMES_HYPHEN).append(Util.intToStr(mMaxCount))
           .append(Util.PARAMES_HYPHEN).append(Util.arrayToString(mDayMask))
           .append(Util.PARAMES_HYPHEN).append(DateUtil.dateToYYYYMMDD(mStartDate.getTime()))
           .append(Util.PARAMES_HYPHEN).append(Util.intToStr(mLunarFlag))
           ;
        
        return super.mLoopParam = sff.toString();
    }
    
    @Override
    public String CheckValidity ()
    {
        if(mLoopDays < 1)
        {
            return "“循环天数”须大于零";
        }

        if(mMaxCount < TimerCalculator.INFINITY_COUNT || mMaxCount == 0)
        {
            return "“循环次数”须大于零";
        }

        if(mLoopDays != mDayMask.length)
        {
            return "循环天数与启动日设置不一致，请点击“启动日”进行设置";
        }
        
        int flag = 0;
        for(char c: mDayMask)
        {
            if(c == '1')
            {
                flag = 1;
                break;
            }
        }
        
        if(flag == 0)
        {
            return "没有设置启动日";
        }
        
        if(mLunarFlag != 0 && mLunarFlag != 1)
        {
            return "农历标识不合法（" + mLunarFlag + "）";
        }
        
        return null;
    }

    public int getLoopDays()
    {
        return mLoopDays;
    }
    
    public void setLoopDays(int d)
    {
        mLoopDays = d;
    }
    
    public int getMaxCount()
    {
        return mMaxCount;
    }
    
    public void setMaxCount(int c)
    {
        mMaxCount = c;
    }
    
    public char[] getDayMask()
    {
        return mDayMask;
    }
    
    public void setDayMask(char[] d)
    {
        if(mDayMask.length != d.length)
        {
            mDayMask = d.clone();
        }
        else
        {
            for(int i = 0; i < d.length; i++)
            {
                mDayMask[i] = d[i];
            }
        }
    }
        
    public Calendar getStartDate()
    {
        return mStartDate;
    }
    
    public void setStartDate(Calendar c)
    {
        mStartDate = (Calendar) c.clone();;
    }
    
    public int getLunarFlag()
    {
        return mLunarFlag;
    }
    
    public void setLunarFlag(int l)
    {
        mLunarFlag = l;
    }

}
